package cn.yaboya.org.controller;

import cn.yaboya.basic.annotation.PreAuthorize;
import cn.yaboya.basic.utils.AjaxResult;
import cn.yaboya.basic.utils.PageList;
import cn.yaboya.org.domain.Department;
import cn.yaboya.org.query.DepartmentQuery;
import cn.yaboya.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: loadById
    * @Description: 根据一个id查询单个
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/6/24 10:05
    * @Parameters: [id]
    * @Return cn.yaboya.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "id查找" )
    @PreAuthorize(name = "根据id查询", sn = "department:get")
    public Department loadById(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /** 
    * @Title: loadAll
    * @Description: 查询全部
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/6/24 10:04  
    * @Parameters: []
    * @Return java.util.List<cn.yaboya.org.domain.Department>
    */
    @GetMapping
    @ApiOperation(value = "查找全部" )

    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
    * @Title:remove
    * @Description: 删除单个
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/6/24 10:06
    * @Parameters:
    * @Return
    */
    @DeleteMapping("{id}")
    @ApiOperation(value = "id删除" )
    @PreAuthorize(name = "根据id删除", sn = "department:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }
    /**
    * @Title: addOrUpt
    * @Description: 增加 或者修改接口
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/6/24 12:41
    * @Parameters: [department]
    * @Return cn.yaboya.basic.utils.AjaxResult
    */
    @ApiOperation(value = "新增或者修改" )
    @PutMapping
    @PreAuthorize(name = "新增或者修改", sn = "department:addOrUpt")
    public AjaxResult addOrUpt(@ApiParam(name="json 对象",required = true) @RequestBody Department department){
        System.out.println(department);
        try {
            // 判断是否有id 没有就新增 有就更改
            if (department.getId() == null){
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }

    /**
     * @Title: queryData
     * @Description: 分页+高级查询
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/24 9:51
     * @Parameters: [departmentQuery]
     * @Return cn.itsource.basic.utils.AjaxResult
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "分页查询", sn = "department:queryData")
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> data = departmentService.queryData(departmentQuery);

        return data;
    }

    /**
    * @Title: batchRem
    * @Description: 批量删除
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/6/25 11:28
    * @Parameters: [ids]
    * @Return cn.yaboya.basic.utils.AjaxResult
    */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "批量删除", sn = "department:batchRem")
    public AjaxResult batchRem(@RequestBody Long[] ids){
        try {
            departmentService.batchRem(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }

    /**
    * @Title: loadAllTree
    * @Description: 查找全部树
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/6/26 9:11
    * @Parameters: []
    * @Return java.util.List<cn.yaboya.org.domain.Department>
    */
    @GetMapping("/tree")
    @ApiOperation(value = "查找全部" )
    @PreAuthorize(name = "查找全部", sn = "department:loadAllTree")
    public List<Department> loadAllTree(){
        return departmentService.loadAllTree();
    }
}
